#!/usr/bin/env python
"测试multiprocessing.Pipe"

from multiprocessing import Pipe, Process


def sender(pipe):
	"通过管道向父进程发送对象"
	pipe.send(['spam'] + [42, 'eggs'])
	pipe.close()


def talker(pipe):
	"通过管道向父进程发送和接受对象"
	pipe.send(dict(name='Bob', spam=42))
	print('talker got:', pipe.recv())


def main():
	parent_pipe, child_pipe = Pipe()
	Process(target=sender, args=(child_pipe,)).start()
	print('parent got:', parent_pipe.recv())
	parent_pipe.close()  # 或者在程序运行完后自动关闭

	parent_pipe, child_pipe = Pipe()
	child_process = Process(target=talker, args=(child_pipe,))
	child_process.start()
	print('parent got:', parent_pipe.recv())
	parent_pipe.send([x_str * 2 for x_str in 'spam'])
	child_process.join()
	print('Parent exit')


if __name__ == '__main__':
	main()
